/*    file: WinVideoWindow.h
 *    desc:
 *   
 * created: 2014-01-21 14:12:14
 *  author: zhengchuanjiang
 * company: 
 */ 


#if !defined WINVIDEOWINDOW_H_
#define WINVIDEOWINDOW_H_

#include "BasicType.h"
#include "VideoWindow.h"
#include "TThread.h"

#include "TCriticalSection.h"
#include "HwndPresenter.h"
#include <deque>


////////////////////////////////////////////////////////////////////////////
namespace av
{

class WinVideoWindow : public VideoWindow
{
public:
    enum Constant
    {
        WM_DRAW_FRAME = WM_USER + 1,
        WM_SET_STATE,

    };

public:

    WinVideoWindow();
    ~WinVideoWindow();

    virtual int open(void* hwnd);
    virtual void close();
    virtual bool isOpen();

    virtual void setVideoWnd(void* hwnd);

    virtual void setState(StreamState state);

    virtual int draw(AVFramePtr frame);

    virtual bool isFullscreen();

    virtual int setFullscreen(bool full);

    virtual int setRatio(int width, int height);

    virtual void getRatio(int& width, int& height);

    virtual int setBackImage(const char* filepath);

    virtual void flush();



protected:

	HWND getHwnd();

	void Invalidate(BOOL bErase);

	void MoveWindow(const RECT& rc, BOOL repaint);

public:
	LRESULT parentWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

protected:
	HWND getParentWnd();

	void subclassWindow(HWND hwnd);
	void unsubclassWindow(HWND hwnd);

	static LRESULT CALLBACK MyWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

	LRESULT OnSize(WPARAM wParam, LPARAM lParam);

	LRESULT OnPaint(WPARAM wParam, LPARAM lParam);
	LRESULT OnEraseBkgnd(WPARAM wParam, LPARAM lParam);
	LRESULT OnLButtonDblClk(WPARAM wParam, LPARAM lParam);

protected:

    void drawBackImage(HDC hdc);
    void drawBackground(HDC hdc);
    void drawFrame();
	void drawBackground();

    void putFullScreen(bool full);

    void onSetState(int state);

    void clearBuffer();

    bool getVideoSize(int& width, int& height);
    RECT getVideoRect();

    void fetchFrame();

protected:
    HWND    m_hWnd;
    int     m_ratioX;
    int     m_ratioY;
    bool    m_enableControl;

    comn::CriticalSection    m_cs;

    HwndPresenter   m_presenter;
    bool    m_fullscreen;
	HWND	m_parentWnd;

    RECT    m_rcLast;

    StreamState    m_state;

    AVFramePtr m_backFrame;
    AVFramePtr m_curFrame;

    //CImage  m_backImage;

    WNDPROC m_superWindowProc;

};


}

////////////////////////////////////////////////////////////////////////////
#endif //WINVIDEOWINDOW_H_

